using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CrazyDogToDo.Entity.Data;

namespace CrazyDogToDo.Service.Services
{
    public class InventoryService
    {
        string myDayName = "我的一天";
        private CrazyDogToDoContext _crazy = new CrazyDogToDoContext();
        /// <summary>
        /// 浏览所有清单
        /// </summary>
        /// <returns></returns>
        public List<Inventory> GetAllInventory(string email)
        {
            var user = _crazy.User.SingleOrDefault(u => u.Email.Equals(email));
            if (user != null)
            {
                return user.Inventory.ToList();
            }
            return null;
        }
        /// <summary>
        /// 清单的创建
        /// </summary>
        /// <param name="email"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public bool CreateInventory(string email, string title)
        {
            var listName = _crazy.Inventory.Where(u => u.Email.Equals(email)).Where(i => i.Title.Equals(title)).SingleOrDefault(t => t.CreateTime.Day == DateTime.Now.Day);/*找到用户下的重复名字的清单*/
            if (listName != null)/*如果用户没有找到 或者 清单名字已经存在*/
            {
                return false;
            }
            else
            {
                var list = new Inventory()
                {
                    Email = email,
                    Title = title,
                    CreateTime = DateTime.Now
                };
                _crazy.Inventory.Add(list);
                _crazy.SaveChanges();
                return true;

            }
        }
            /// <summary>
            /// 修改清单名字
            /// </summary>
            /// <param name="email"></param>
            /// <param name="newTitle"></param>
            /// <returns></returns>
            public bool ModifyInventoryName(string email, string newTitle)
            {
                var listName = _crazy.Inventory.Where(u => u.Email.Equals(email)).Where(i => i.Title.Equals(newTitle)).SingleOrDefault(t => t.CreateTime.Day == DateTime.Now.Day);/*找到用户下的重复名字的清单*/
                if (listName != null)/*如果用户没有找到 或者 清单名字已经存在*/
                {
                    return false;
                }
                else
                {
                    listName.Title = newTitle;
                    _crazy.SaveChanges();
                    return true;
                }
            }
            /// <summary>
            /// 删除清单
            /// </summary>
            /// <param name="email"></param>
            /// <param name="id"></param>
            public void DeleteInventory(int id)
            {
                var list = _crazy.Inventory.SingleOrDefault(n => n.Id == id);//找到要删除的清单对象
                _crazy.Inventory.Remove(list);
                _crazy.SaveChanges();
            }
            /// <summary>
            /// 查找清单
            /// </summary>
            /// <param name="createTime"></param>
            /// <returns></returns>
            public bool SearchInventory(string email, DateTime createTime)
            {
                var inventory = _crazy.Inventory.Where(u => u.Email.Equals(email))
                    .Where(n => n.Title.Equals(myDayName))
                    .SingleOrDefault(I => I.CreateTime.Day == createTime.Day);
                if (inventory == null)
                {
                    return false;
                }
                else
                    return true;
            }
        }
    }
